package com.blinddate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.blinddate.mapper.UserInfMapper;
import com.blinddate.mapper.UserMapper;
import com.blinddate.pojo.dto.UserInfDto;
import com.blinddate.pojo.entity.User;
import com.blinddate.pojo.entity.UserInf;
import com.blinddate.pojo.result.Result;
import com.blinddate.service.UserInfService;
import com.blinddate.service.UserService;
import com.blinddate.utils.AliOssUtil;
import com.blinddate.utils.WeChatUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@Slf4j
@Api(tags = "假信息相关接口")
@Transactional
@CrossOrigin
public class FakeController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfMapper userInfMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private WeChatUtil weChatUtil;
    @ApiOperation("提交假信息")
    @PostMapping("/fakeuser")
    public Result fake(@RequestBody UserInfDto userInfDto) throws IOException {

        User user=User.builder()
                .count(0)
                .identity(0)
                .build();
        userMapper.fakeinsert(user);
        Long userid=user.getId();

        UserInf userInf =new UserInf();
        BeanUtils.copyProperties(userInfDto,userInf);
        userInf.setUserId(userid);
        userInf.setCodeSrc(weChatUtil.getwxcode(userid));
        userInfMapper.insert(userInf);

        log.info("生成用户id为：{}的pdf文件",userid);
        LambdaQueryWrapper<UserInf> lqw=new LambdaQueryWrapper<UserInf>();
        lqw.eq(UserInf::getUserId,userid);
        String s = userid.toString()+".pdf";
        String pdfpath=aliOssUtil.pdf(userInf,s);
        LambdaUpdateWrapper<User> wrapper=new LambdaUpdateWrapper<>();
        wrapper.set(User::getPdfPath, pdfpath);
        wrapper.eq(User::getId, userid);
        userService.update(wrapper);
        return Result.success();
    }
}
